(function () {
    'use strict';

    /**
     * 权限和角色验证
     */
    angular
        .module('SURF')
        .factory('AuthService', AuthService);

    AuthService.$inject = ['CookieStoreService', 'COOKIE_STORE_KEYS'];

    function AuthService(CookieStoreService, COOKIE_STORE_KEYS) {
        var roleAllowed = function (roles) {
            for (var i = 0; i < roles.length; i++) {
                if (roles[i] == CookieStoreService.getValue(COOKIE_STORE_KEYS.ROLE)) {
                    return true;
                }
            }
            return false;
        };
        var permissionAllowed = function (permissions) {
            if (permissions.length > 0) {
                var flag = 0;
                for (var i = 0; i < permissions.length; i++) {
                    for (var j = 0; j < CookieStoreService.getValue(COOKIE_STORE_KEYS.PERMISSIONS).length; j++) {
                        if (CookieStoreService.getValue(COOKIE_STORE_KEYS.PERMISSIONS)[j] == permissions[i]) {
                            flag = 1;
                        }
                    }
                }
                if (flag == 0) {
                    return false;
                }
            }
            return true;
        };
        return {
            isAllowed: function (roles, permissions) {
                return roleAllowed(roles) && permissionAllowed(permissions);
            }

        }
    }

    /**
     * response拦截器
     */
    angular
        .module('SURF')
        .factory('AuthInterceptor', AuthInterceptor);

    AuthInterceptor.$inject = ['$q', '$location', 'CookieStoreService', 'COOKIE_STORE_KEYS','$translate'];

    function AuthInterceptor($q, $location, CookieStoreService, COOKIE_STORE_KEYS,$translate) {
        return {
            request: function (config) {
                config.headers['mobileFlag'] = '0';
                if (CookieStoreService.getValue(COOKIE_STORE_KEYS.TOKEN_TYPE) != null && CookieStoreService.getValue(COOKIE_STORE_KEYS.ACCESS_TOKEN) != null) {
                    config.headers['Authorization'] = CookieStoreService.getValue(COOKIE_STORE_KEYS.TOKEN_TYPE) + " " + CookieStoreService.getValue(COOKIE_STORE_KEYS.ACCESS_TOKEN);
                }
                return config;
            },
            responseError: function (response) {
                // 401 : Unauthorized
                if (response.status === 401) {
                    $location.path("/curve");
                }else if(response.status === 0){
                    showCommonDialog($translate.instant("SERVER_ERROR"),2);
                }
                return $q.reject(response);
            }
        };
    }

    /**
     * cookie存储
     */
    angular
        .module('SURF')
        .factory('CookieStoreService', CookieStoreService);

    CookieStoreService.$inject = ['$cookieStore', 'COOKIE_STORE_KEYS'];

    function CookieStoreService($cookieStore, COOKIE_STORE_KEYS) {

        this.create = function (userName, role,cTaiId, permissions, accessToken, tokenType) {
            $cookieStore.put(COOKIE_STORE_KEYS.USER_NAME, userName);
            $cookieStore.put(COOKIE_STORE_KEYS.USER_ID, cTaiId);
            $cookieStore.put(COOKIE_STORE_KEYS.ROLE, role);
            $cookieStore.put(COOKIE_STORE_KEYS.PERMISSIONS, permissions);
            $cookieStore.put(COOKIE_STORE_KEYS.ACCESS_TOKEN, accessToken);
            $cookieStore.put(COOKIE_STORE_KEYS.TOKEN_TYPE, tokenType);
        };

        this.destroy = function () {
            $cookieStore.remove(COOKIE_STORE_KEYS.USER_NAME);
            $cookieStore.remove(COOKIE_STORE_KEYS.ROLE);
            $cookieStore.remove(COOKIE_STORE_KEYS.USER_ID);
            $cookieStore.remove(COOKIE_STORE_KEYS.PERMISSIONS);
            $cookieStore.remove(COOKIE_STORE_KEYS.ACCESS_TOKEN);
            $cookieStore.remove(COOKIE_STORE_KEYS.TOKEN_TYPE);
        };

        this.getValue = function (cookieKey) {
            return $cookieStore.get(cookieKey);
        };
        return this;
    }
})();
